﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using FrameWork.Lib;
using FrameWork.WinUI.Common;

namespace AnimeWatchMax
{
    public partial class frmTitleList : DockContent
    {
        public frmTitleList()
        {
            InitializeComponent();
            mListViewSorter = new FrameWork.WinUI.Common.ListViewSorter(lstView);
            mListViewSorter.ColumnComparerCollection.Add(lstView.Columns[3].Text, new FrameWork.WinUI.Common.DateComparer());
            mListViewSorter.ColumnComparerCollection.Add(lstView.Columns[4].Text, new FrameWork.WinUI.Common.DateComparer());
        }

        public UIManager UIManager = null;
        public Guid Key;
        private FrameWork.WinUI.Common.ListViewSorter mListViewSorter = null;

        public void UpdateViewer()
        {
            this.lstView.Items.Clear();

            foreach (var tTitle in UIManager.Data.TitleList)
            {
                AddOrUpdateItem(tTitle);
            }
        }

        public void AddOrUpdateItem(DataObjects.Title pTitle)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new EventManager.DelegateAddTitleItem(AddOrUpdateItem), pTitle);
            }
            else
            {
                var tkey = pTitle.DataObjectGUID.ToString();

                ListViewItem tItem = null;

                if (!lstView.Items.ContainsKey(tkey))
                {
                    tItem = lstView.Items.Add(tkey, pTitle.Name, 0);
                    tItem.SubItems.Add(pTitle.State.ToString());
                    tItem.SubItems.Add(pTitle.Status.ToString());
                    tItem.SubItems.Add(pTitle.ReleasedDate.StringValueOrEmpty("MM/dd/yyyy"));
                    tItem.SubItems.Add(pTitle.Updated.StringValueOrEmpty("MM/dd/yyyy"));
                }
                else
                {
                    tItem = lstView.Items[tkey];
                    tItem.SubItems[0].Text = pTitle.Name;
                    tItem.SubItems[1].Text = pTitle.State.ToString();
                    tItem.SubItems[2].Text = pTitle.Status.ToString();
                    tItem.SubItems[3].Text = pTitle.ReleasedDate.StringValueOrEmpty("MM/dd/yyyy");
                    tItem.SubItems[4].Text = pTitle.Updated.StringValueOrEmpty("MM/dd/yyyy");
                }

                tItem.Tag = pTitle.DataObjectGUID;


                if (pTitle.Status == TitleStatusType.Licensed)
                {
                    tItem.BackColor = Color.Maroon;
                }
                else if(pTitle.State == TitleStateType.New)
                {
                    tItem.BackColor = Color.Brown;
                }

            }
        }

        private void lstView_SelectedIndexChanged(object sender, EventArgs e)
        {
            var tSelectedDataObject = lstView.SelectedDataObjectByGuid(UIManager.Data.TitleList.ToList<FrameWork.Lib.DataObjects.DataObjectBase>());
            if (tSelectedDataObject != null)
            {
                UIManager.ViewTitle((DataObjects.Title)tSelectedDataObject);
            }
        }

    }
}
